﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace AcctrueWMS.Foundation.Data
{
    public class ForeignKeyAnalyser
    {
        /// <summary>
        /// 从DataModel关系串中获取外键
        /// </summary>
        /// <param name="relationStr"></param>
        /// <param name="modelName"></param>
        /// <returns></returns>
        public static string GetForeightKey(string relationStr, string modelName)
        {
            Regex reg = new Regex(@"(?<model1>[\w\d]+)\.(?<colName1>[\w\d]+)\s*=\s*(?<model2>[\w\d]+)\.(?<colName2>[\w\d]+)");
            MatchCollection m = reg.Matches(relationStr);
            string foreightKey = "";
            foreach (Match dm in m)
            {
                //foreightKey += (dm.Groups["model1"].Value == modelName) ? dm.Groups["colName1"].Value : dm.Groups["colName2"].Value;
                if (dm.Groups["model1"].Value == modelName)
                    foreightKey += dm.Groups["colName1"].Value;
                if (dm.Groups["model2"].Value == modelName)
                    foreightKey += dm.Groups["colName2"].Value;
                foreightKey += ",";
            }
            return foreightKey.TrimStart(',').TrimEnd(',');
        }
    }
}
